import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
/**
 * NB rename this class so that it has a unique name on the Robot
 * @author 

 * @version 1.0
 */
public class Robot { 
    public static void main(String[] aArg) throws Exception {
        /**
         * Making instances of all parts.
         */
        BT bluetooth = new BT();
        Control control = new Control();
        AI ai = new AI();
        Component sonic = new Ultrasonic();
        
        /**
         * Main algorithm.
         */
        
        while(bluetooth.isConnected() != true) {
            LCD.clear();
            LCD.drawString("Waiting for connection.",0,0);
            LCD.refresh();
            bluetooth.BTConnect();
        }
          
        if(bluetooth.isConnected()) {
            LCD.clear();
            LCD.drawString("You can steer me.",0,0);
            LCD.refresh();
            LCD.clear();
            
            /**
             * Ultrasonic test
             */
            while(true) {
                Thread.sleep(200);
                LCD.clear();
                LCD.drawInt(sonic.getValue(),0,0);
                LCD.refresh();
                Thread.sleep(500);
            }
           /**
            * fetching commands
            */
           /* while(true) {
                try {
                    String inputStr = bluetooth.getInputStream();
                    control.doCommand(inputStr, ai);
                } catch(IOException e) {
                    LCD.clear();
                    LCD.drawString("Connection lost.",0,0);
                    LCD.refresh();
                    bluetooth.BTClose();
                    break;
                }
            }*/
            
            //unreachable statement --> bluetooth.BTClose();
            
        } else {
            LCD.clear();
            LCD.drawString("You cannot steer me.",0,0);
            LCD.refresh();
            //Running future AI here.
        }
        
    }
   
}